package com.example.demo01.base.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.io.IOException;
import java.util.List;

/**
 * @Description: 自定义配置类实现JavaBean注解形式配置
 * WebMvcConfigurerAdapter是过时的方法 可以用WebMvcConfigurer或者WebMvcConfigurationSupport代替
 * @Author : hzq
 * @Date : 2020-04-28
 * @Project: demo01
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    /**
     * Description: 跨域CORS配置
     *
     * @param registry:
     * @return void
     * @author houzq
     * @data 2020/10/15
     **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/cors/**")
                .allowedHeaders("*")
                .allowedMethods("post", "get")
                .allowedOrigins("*");
    }

    /**
     * Description: 视图控制器配置
     *
     * @param registry:
     * @return void
     * @author houzq
     * @data 2020/10/15
     **/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/index");
    }

    /***
     * @Description: 配置请求视图映射
     * @MethodsName: resourceViewResolver
     * @Author: hzq
     * @Data  : 2020/4/28
     * @Param : []
     * @return: org.springframework.web.servlet.view.InternalResourceViewResolver
     * @throws:
     **/
    public InternalResourceViewResolver resourceViewResolver(){
        InternalResourceViewResolver irvr = new InternalResourceViewResolver();
        irvr.setPrefix("/WEB-INF/jsp/");
        irvr.setSuffix(".jsp");
        return irvr;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(resourceViewResolver());
    }

    /***
     * @Description: 消息内容转换配置
     *               配置fastJson返回json转换
     *               一般针对于Api接口服务程序，配置在请求返回时内容采用什么转换器进行转换，最常用到是fastJson的转换
     *@param converters
     * @MethodsName: configureMessageConverters
     * @Author: hzq
     * @Data  : 2020/4/28
     * @Param : [converters]
     * @return: void
     * @throws:
     **/
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建fastJson消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );
        // 将fastJson添加到视图消息转换器列表内
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }
}
